﻿using System;
using System.IO;
using System.Net;
using KPI.Data;
using KPI.Entity;
using TYBase;

namespace KPI.Api
{
    public class LearnFileApi
    {
        public static R<bool> AddFile(string filePath, Tbl_LearnFile file)
        {
            try
            {

                LocalHttp http = new LocalHttp();
                if (!http.SetBaseUri(KPIData.ServerURL)
                    .SetUrl($"/learnFile/add?{file.AsQueryData()}")
                    .SetPost()
                    .UploadFile(filePath, out string data))
                {
                    return R<bool>.Fail(data);
                }
                return data.AsModel<R<bool>>();
            }
            catch (Exception ex)
            {
                return R<bool>.Fail("创建失败!", ex);
            }
        }
        public static R<bool> EditFile(string filePath, Tbl_LearnFile file)
        {
            try
            {

                LocalHttp http = new LocalHttp();
                if (!http.SetBaseUri(KPIData.ServerURL)
                    .SetUrl($"/learnFile/editWithNewFile?{file.AsQueryData()}")
                    .SetPost()
                    .UploadFile(filePath, out string data))
                {
                    return R<bool>.Fail(data);
                }
                return data.AsModel<R<bool>>();
            }
            catch (Exception ex)
            {
                return R<bool>.Fail("创建失败!", ex);
            }
        }
        public static R<bool> EditFile(Tbl_LearnFile file)
        {
            try
            {

                LocalHttp http = new LocalHttp();
                if (!http.SetBaseUri(KPIData.ServerURL)
                    .SetUrl($"/learnFile/edit")
                    .SetPost()
                    .SetData(file.JsonAsString())
                    .Excute(out string data))
                {
                    return R<bool>.Fail(data);
                }
                return data.AsModel<R<bool>>();
            }
            catch (Exception ex)
            {
                return R<bool>.Fail("创建失败!", ex);
            }
        }
        public static R<object> Download(string filePath, int file_id)
        {
            try
            {
                string url = $"{KPIData.ServerURL}/learnFile/downloadFile?file_id={file_id}";
                using (FileStream fileStream = new FileStream(filePath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
                {
                    // 设置参数
                    HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
                    request.Timeout = 30000;
                    request.ReadWriteTimeout = 5000;
                    //发送请求并获取相应回应数据
                    HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                    Stream responseStream = response.GetResponseStream();
                    //创建本地文件写入流
                    byte[] bArr = new byte[1024];
                    int size = responseStream.Read(bArr, 0, (int)bArr.Length);
                    while (size > 0)
                    {
                        fileStream.Write(bArr, 0, size);
                        size = responseStream.Read(bArr, 0, (int)bArr.Length);
                    }
                    responseStream.Close();
                    return R<object>.OK();
                }
            }
            catch (Exception ex)
            {
                return R<object>.Fail("下载文件失败!", ex);
            }
        }

        public static RPage<LearnFileVO> ListFile(PageInfo page, LearnFileDto fileVO)
        {
            try
            {
                LocalHttp http = new LocalHttp();
                if (!http.SetBaseUri(KPIData.ServerURL)
                    .SetGet()
                    .SetUrl($"/learnFile/listFile?{page.AsQueryData()}&{fileVO.AsQueryData()}")
                    .Excute(out string data))
                {
                    return RPage<LearnFileVO>.Fail(data);
                }
                return data.AsModel<RPage<LearnFileVO>>();
            }
            catch (Exception ex)
            {
                return RPage<LearnFileVO>.Fail("获取失败!", ex);
            }
        }

    }
}
